<?php
namespace app\h5api\controller;

use app\common\model\Collection;
use app\common\model\Comment;
use app\common\model\Commit;
use app\common\model\Footprint;
use app\common\model\ProductImage;
use app\common\model\ProductStock;
use app\common\model\ProductsType as ProductsTypeModel;
use app\common\model\Shops;
use app\common\model\ShopsBanner;
use app\common\model\User;
use app\h5api\validate\Product;

class Products extends Base
{

    /**
     * @route('h5/products/productinfo')
     * 商品详情
     */
    public function ProductInfo()
    {
        $ProductModel = new \app\common\model\Products();
        $ImageModel = new ProductImage();
        $CollectionModel = new Collection();
        $CommentModel = new Comment();
        $shopModel = new Shops();
        $userModel = new User();
        $FootModel = new Footprint();
        $StockModel = new ProductStock();

        $productId = input('product_id');
        $field = "product_id,type_id,shop_id,product_name,describe,price,show,mprice,salecount,createat,express_fee,integral,imageurl";
        $product = $ProductModel->getOne(['product_id' => $productId],$field);
        if(!$product){
            $this->result('',-1,'没有此商品信息');
        }

        // 店铺
        $shop = $shopModel->getOne(['shop_id' => $product['shop_id']],'shop_id,shop_name,imagesurl');

        // 最新的一条评价
        $commentfield = "comment_id,content,star,user_id,product_service_id,images,add_time";
        $comment = $CommentModel->getOne(['product_service_id' => $productId,'type' => 1, 'is_show' => 1],$commentfield);
        if($comment){
            $comment['image'] = explode(",", $comment['images']);
            unset($comment['images']);
            $comment['user'] = $userModel->getOne(['user_id' => $comment['user_id']],'user_id,nick_name,avatar');
        }

        // 商品轮播图
        $imgcond['product_id'] = $productId;
        $imgcond['is_show'] = 1;
        $ProductImage = $ImageModel->get_list($imgcond,'image_id,url','sort asc');

        // 商品是否收藏
        $is_coll = 1;
        $coll = $CollectionModel->getOne(['product_id' => $productId, 'user_id' => $this->_userid],'c_id');
        if($coll){
            $is_coll = 2;
        }

        // 添加足迹
        $FootModel->record($this->_userid,$productId);

        // 规格
        $cond['product_id'] = $productId;
        $data = $StockModel->get_list($cond,'p_s_id,p_s_name,stock');

        $tvar = [
            'product'   => $product,
            'shop'  => $shop,
            'comment'   => $comment,
            'image' => $ProductImage,
            'is_coll'   => $is_coll,
            'stock'  => $data
        ];
        $this->result($tvar,1);
    }





























    /**
     * @route('h5/ProductList')
     * 商品列表
     */
    public function ProductList()
    {
//        $ProductsTypeModel = new ProductsTypeModel();
        $ProductModel = new \app\common\model\Products();
        $limit = input('get.limit') ? input('get.limit') : 10;
        $validate = new Product();
        $ssort = input('get.ssort');
        $nsort = input('get.nsort');
        if(!$validate->scene('paixu')->check(['ssort'=>$ssort,'nsort'=>$nsort])){
            $this->result('',-1,$validate->getError());
        }

        $pwhere = [
            "product_name" => input('get.product_name'),
            "shop_id" => input('get.shop_id'),
            "type_id" => input('get.type_id'),
            "state" => 1,
            "approved" => 2,
            "shop_state" => 1,
            "shop_approved" => 1,
            "is_del" => 1,
            "class_id" => 1,
        ];
        $orderby = [
            "salecount" => $ssort,
            "createat" => $nsort,
        ];
//        var_dump($orderby);die;
        $item = $this->getfieldstring('p.*');
        $products = $ProductModel->get_product_list($pwhere,$orderby,$item,$limit);
        $this->result($products,1,'返回成功');
    }

    /**
     * @route('h5/GetProductDetail')
     * 普通商品获取详情
     */
    public function GetProductDetail()
    {
        $ProductModel = new \app\common\model\Products();
        $FootModel = new Footprint();
        $productid = input('get.product_id');
        $data = [
            'product_id' => $productid,
            'class_id' => 1,
        ];
        $item = '*';
        $item = $this->getfieldstring($item);
        $products = $ProductModel->get_product_detail($data,$item);
        $FootModel->record($this->_userid,$productid);
        $data = [
            "products" => $products,
        ];
        $this->result($data,1,'返回成功');
    }

    /**
     * @route('h5/GetProductCommit')
     * 商品评价列表
     */
    public function GetProductCommit()
    {
        $model = new Commit();
        $productid = input('get.product_id');
        $type = input('get.type',1);

        $commits = $model->get_product_commit($productid,$type,$this->limit);
        $this->result($commits,1,'返回成功');
    }

    /**
     * @route('h5/CollectProduct')
     * 用户收藏商品
     */
    public function CollectProduct()
    {
        $CollectModel = new Collection();
        $productid = input('post.product_id');
        $res = $CollectModel->record($this->_userid,$productid);
        if(!$res){
            $this->result('',-1,'收藏操作失败');
        }
        $this->result('',1,'成功');
    }

    /**
     * @route('h5/GetHotProduct')
     * 获取热门商品列表
     */
    public function GetHotProduct()
    {
        $ProductModel = new \app\common\model\Products();
        $shopid = input('get.shop_id');
        $condition = [
            "shop_id" => $shopid,
            "state" => 1,
            "approved" => 2,
            "is_del" => 1,
        ];
        $item = "p.*";
        $item = $this->getfieldstring($item);
        $hots = $ProductModel->get_hot_list($condition,$item,$limit = 9);
        $this->result($hots,1,'成功');
    }

    /**
     * @route('h5/GetUncommonProduct')
     * 获取非普通商品列表
     */
    public function GetUncommonProduct()
    {
        $ProductModel = new \app\common\model\Products();
        $shopid = input('get.shop_id');
        $classname = input('get.class_name');
        $limit = input('get.limit') ? input('get.limit') : 9;
        switch($classname){
            case "exchange":
                $class_id = 2;
                break;
            case "love":
                $class_id = 3;
                break;
            case "team":
                $class_id = 4;
                break;
            default:
                $this->result([],-1,'类型错误');
                break;
        }
        $condition = [
            "shop_id" => $shopid,
            "state" => 1,
            "approved" => 2,
            "is_del" => 1,
            "shop_approved" => 1,
            "shop_state" => 1,
            "stock_del" => 1,
            "class_id" => $class_id,
        ];
        $ssort = input('get.ssort');
        $nsort = input('get.nsort');
        $validate = new Product();
        if(!$validate->scene('paixu')->check(['ssort'=>$ssort,'nsort'=>$nsort])){
            $this->result('',-1,$validate->getError());
        }
        $orderby = [
            "salecount" => $ssort,
            "createat" => $nsort,
        ];
        $item = 'p.*,ps.p_s_id,ps.p_s_name,ps.g_m_details,ps.g_m_paper,ps.stock,s.shop_name';
        $item = $this->getfieldstring($item);
        $hots = $ProductModel->get_uncommon_list($condition,$orderby,$item,$limit);
        $this->result($hots,1,'成功');
    }

    /**
     * @route('h5/GetTeamProductDetail')
     * 团餐商品获取详情
     */
    public function GetTeamProductDetail()
    {
        $ProductModel = new \app\common\model\Products();
//        $FootModel = new Footprint();
        $productid = input('get.product_id');
        $data = [
            'product_id' => $productid,
            'class_id' => 4,
            'stock_id' => input('get.stock_id'),
        ];
        $item = 'p.*,ps.p_s_name,g_m_details,g_m_paper,stock,ps.p_s_id';
        $item = $this->getfieldstring($item);
        $products = $ProductModel->get_uncommon_product_detail($data,$item);
//        $FootModel->record($this->_userid,$productid);
        $data = [
            "products" => $products,
        ];
        $this->result($data,1,'返回成功');
    }

    function getfieldstring($item){
        $item .= ',p.mprice as sale_price';
        return $item;
    }

    /**
     * @route('h5/Shopinfo')
     * 商铺详情
     */
    public function Shopinfo()
    {
        $shopModel = new Shops();
        $shop_id = input('get.shop_id');
        $shop = $shopModel->getOneShop($shop_id,'*');
        $data['shop'] = empty($shop)?null:$shop->toArray();
        $data['shop_banner'] = [];
        if(!empty($data['shop']) && ($data['shop']['shop_type'] == 1 || $data['shop']['shop_type'] == 3)){
            $bannerModel = new ShopsBanner();
            $banners = $bannerModel->get_list([['shop_id','=',$shop_id],['type','=',1]],'sort','*',3);
            $banners = $banners->toArray();
            $data['shop_banner'] = $banners['data'];
        }
        $this->result($data,1,'返回成功');
    }
}
